<!-- 横向柱状图 -->
<template>
  <div>
    <div>【大区数据信息】</div>
    <div class="w-full h-full" ref="target"></div>
  </div>
</template>
<script setup>
import { defineProps, onMounted, ref, watch } from "vue";
import * as echarts from "echarts";

const props = defineProps({
  data: {
    type: Object,
    required: true,
  },
});

// 1.初始化echarts 实例
const target = ref(null);
let mChart = null;
onMounted(() => {
  mChart = echarts.init(target.value);
  renderChart();
});
// 2.构建option配置对象
const renderChart = () => {
  const options = {
    // x轴的展示数据
    xAxis: {
      // x轴不显示
      show: false,
      type: "value",
      max: function (value) {
        return parseInt(value.max * 1.2);
      },
    },
    // y轴的展示数据
    yAxis: {
      type: "category",
      data: props.data.regions.map((item) => item.name),
      inverse: true,
      // 不展示刻线
      axisLine: {
        show: false,
      },
      // 不展示刻度
      axisTick: {
        show: false,
      },
      axisLabel: {
        color: "#9eb1c8",
      },
    },
    // 图标绘制的位置，对应上下左右
    grid: {
      top: 0,
      right: 0,
      bottom: 0,
      left: 0,
      containLabel: true,
    },
    // 核心配置
    series: [
      {
        type: "bar",
        data: props.data.regions.map((item) => ({
          name: item.name,
          value: item.value,
        })),
        showBackground: true,
        backgroudStyle: {
          color: "rgba(180,180,180,0.2)",
        },
        itemStyle: {
          color: "#479AD3",
          barBorderRadius: 5,
          shadowColor: "rgba(0,0,0,0.3)",
        },
        barWidth: 12,
        label: {
          show: true,
          position: "right",
          textStyle: {
            color: "#fff",
          },
        },
      },
    ],
  };
  // 3.通过实例.setOptions(option)
  mChart.setOption(options);
};
watch(
  () => props.data,
  () => {
    renderChart();
  }
);
</script>
<style scoped lang="scss"></style>
